const userService = require('../service/userService')
const express = require('express')
const router = new express.Router();

// 或者post
router.get('/login',(req,res)=>{
    //res.send('userRouter --> login')

   // console.log(req.query)
    const user = {
        username:req.query.username,
        password:req.query.password
    }
    /* 同步 */
    // const resMag = userService.login(user)
    // // const resMag={
    // //     flag:true
    // // }
    // res.json(resMag)

    /* 异步 */
    userService.login(user,function (resMsg) {
        res.json(resMsg)
    })

})

router.post('/add',(req,res)=>{
    const user={
        username:req.body.username,
        password: req.body.password
    }
    userService.add(user,function (resMag) {
        console.log(global.db)
        res.json(resMag)
    })
})
router.post('/save',(req,res)=>{

    console.log(req.body);
    const user = {
        username: req.body.username,
        password: req.body.password,
        index:req.body.index
    }
    userService.save(user,function (resMsg){
        console.log(global.db);
        res.json(resMsg)
    })
})
router.get('/list',(req,res)=>{
    //res.send('userRouter --> list')
    userService.list(function (users) {
        res.json(users)
    })
})

module.exports=router;
